package com.ruoyi.miniapp.utils;

import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.Base64;

public class ImageUtils {

    public static String convertToBase64(MultipartFile file) throws IOException {
        // 读取文件内容为字节数组
        byte[] imageBytes = file.getBytes();
        // 使用Java 8的Base64工具类进行编码
        String base64Encoded = Base64.getEncoder().encodeToString(imageBytes);
        // 添加Data URL前缀（可选，根据前端需求）
        String contentType = file.getContentType();
        if (contentType != null) {
            return "data:" + contentType + ";base64," + base64Encoded;
        }
        return base64Encoded;
    }
}
